﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;


namespace PDAMain.Forms.CustomerWork
{
    public partial class CustomerTransfer : Form
    {
        public string customerID;
        public CustomerTransfer()
        {
            InitializeComponent();
        }

        private void CustomerTransfer_Load(object sender, EventArgs e)
        {
            BindData();
        }

        //加载数据
        private void BindData()
        {
            //獲取數據
            DataTable dt = new DataTable();
            dt = DataHelper.CustomerWorkDao.getDetail(customerID).Tables[0];
            if (dt.Rows.Count == 0)
                return;

            lblActiveID.Text = dt.Rows[0]["CWOrder_No"].ToString();
            lblStatus.Text = DataHelper.CustomerWorkDao.getShow(dt.Rows[0]["STATUS"].ToString());
            lblAddress.Text = dt.Rows[0]["Address"].ToString();
            lblContactName.Text = dt.Rows[0]["User_Name"].ToString();
            lblContactPhone.Text = dt.Rows[0]["User_Phone"].ToString();
            lblPriority.Text = dt.Rows[0]["Event_Level"].ToString();
            lblDescription.Text = dt.Rows[0]["DESCRIPTION"].ToString();
            lblRemark.Text = dt.Rows[0]["Comment"].ToString();

            //綁定下拉框
            comTransferReason.Items.Clear();
            comTransferReason.Items.Insert(0, "能力不足");
            comTransferReason.Items.Insert(1, "时间不足");
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (comTransferReason.SelectedItem.ToString() == string.Empty)
            {
                MessageBox.Show("请选择转单原因");
                return;
            }
            if (MessageBox.Show("你确定要执行此操作吗？", "提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            string strActiveID;
            string strStatus;
            string strAddress;
            string strContactName;
            string strContactPhone;
            string strPriority;
            string strDescription;
            string strRemark;
            string strTransferReason;

            strActiveID = lblActiveID.Text.ToString();
            strStatus = lblStatus.Text.ToString();
            strAddress = lblAddress.Text.ToString();
            strContactName = lblContactName.Text.ToString();
            strContactPhone = lblContactPhone.Text.ToString();
            strPriority = lblPriority.Text.ToString();
            strDescription = lblDescription.Text.ToString();
            strRemark = lblRemark.Text.ToString();
            strTransferReason = comTransferReason.SelectedItem.ToString();

            //保存數據
            if (DataHelper.CustomerWorkDao.deal(strActiveID, "2", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), strTransferReason))
            {
                MessageBox.Show("成功同步数据至远程数据库");
            }
            else
            {
                if (DataHelper.CustomerWorkDao.insertLacolTable(strActiveID, "2", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), strTransferReason))
                {
                    MessageBox.Show("同步数据失败，保存至本地数据库");
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
            backToList();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //返回
            CustomerDetail detail = new CustomerDetail();
            detail.customerID = customerID;
            detail.Show();
            this.Close();
        }

        private void backToList()
        {
            CustomerList list = new CustomerList();
            list.Show();
            this.Close();
        }
    }
}